﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WPF_Adorner.Adorners
{
    public class Adorner2 : System.Windows.Documents.Adorner
    {
        private VisualCollection _visualCollection;
        private Button _btn;
        private Canvas _canvas;

        public Adorner2(UIElement adornedElement) : base(adornedElement)
        {
            //区别处
            _visualCollection = new VisualCollection(adornedElement);

            _btn = new Button() { Width = 25, Height = 25 };

            _canvas = new Canvas();

            _canvas.Children.Add(_btn);

            _visualCollection.Add(_canvas);

            _btn.Click += _btn_Click;
        }

        private void _btn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("我是Adorner2中的按钮");
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return _visualCollection.Count;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return _visualCollection[index];
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            _canvas.Arrange(new Rect(finalSize));

            _btn.Margin = new Thickness(finalSize.Width, -finalSize.Height / 2, 0, 0);

            return base.ArrangeOverride(finalSize);
        }
    }
}